<?php
namespace app\index\controller;

use think\Db;
use app\common\controller\Base64;
use app\common\model\user\Users;
use app\common\model\user\UserPay;
use think\Config;
use think\Exception;
use app\common\model\user\UserFeed;

class User extends Base
{
	/**
	 * 获取用户信息
	 * @return unknown
	 */
	public function getUserInfo()
	{
		$user = Users::get($this->user_id);
		$user->team_num = Users::where(['tjuser' => $user->no])->count();
		return jsonSuccess($user);
	}
	
	/**
	 * 获取下级用户
	 * @return unknown
	 */
	public function getUserDown()
	{
		$user_id = $this->request->post('user_id');
		$user_no = Users::where(['id' => $user_id])->value('no');
		$down_list = Users::where(['tjuser' => $user_no])->order('id desc')->paginate()->toArray();
		foreach ($down_list['data'] as &$rows) {
			$rows['team_num'] = Users::where(['tjuser' => $rows['no']])->count();
		}
		return jsonSuccess($down_list);
	}
	
    /**
     * 获取用户实名信息
     */
    public function UserRealname(){
        $list=Users::get($this->user_id);
        return jsonSuccess($list);
    }
    /**
     * 实名认证操作
     */
    public function realName(){
        $data = $this->reqeust->post('');
        if(!$data['real_name'])return jsonError('请输入真实姓名');
        if(!$data['idcard'])return jsonError('请输入身份证');
        if(!$data['idcard_up'])return jsonError('请上传身份证正面');
        if(!$data['idcard_down'])return jsonError('请上传身份证反面');
        //验证身份证
        if($this->idcard_checksum18($data['idcard']) === false)return jsonError('请输入真实身份证');
        //不可重复认证
        $user=Users::where(['idcard'=>$data['idcard']])->value('id');
        if($user)return jsonError('该身份已被实名');
        //实名操作
        Users::where(['id'=>$this->user_id])->update($data);
        return jsonSuccess('实名成功');
    }
    /**
     * 判断身份证号码
     * @param $idcard_base
     * @return bool|mixed
     */
    function idcard_checksum18($idcard){
        if (strlen($idcard) != 18){ return false; }
        $idcard_base = substr($idcard, 0, 17);
        if (idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 上传图片
     */
    public function idcard()
    {
    	//获取客户端传递的临时文件
    	if(!empty($_FILES['file'])){
    		$file = $_FILES['file'];
    		$upload = new Base64();
    		try {
    			$res = $upload->uniAppUpload(Config::get('path.idcard'), $file);
    			if(!$res)throw new Exception('图片上传失败');
    		} catch (Exception $e) {
    			return jsonError($e->getMessage());
    		}
    		return jsonSuccess($res);
    	}
    }
    
    /**
     * 获取用户收款方式
     * @param boolean $return
     * @return unknown
     */
    public function getUserPay($return = false)
    {
    	$pay_info = UserPay::where(['user_id' => $this->user_id])->find();
    	return $return ? $pay_info : jsonSuccess($pay_info);
    }
    
    /**
     * 设置收款方式
     * @throws Exception
     * @return unknown
     */
    public function payWay(){
        $data = $this->reqeust->post('');
        try {
        	//获取用户的收款信息
        	$pay_info = $this->getUserPay(true);
        	//判断数据的合法性,检测银行卡的数据
        	if($data['bank_name'] || $data['bank_no'] || $data['bank_addr'] || $data['bank_mobile'] || $data['bank_user']) {
        		if(!$data['bank_name'])throw new Exception('请输入银行名称');
        		if(!$data['bank_no']) throw new Exception('请输入银行卡号');
        		if(!$data['bank_addr']) throw new Exception('请输入开户行');
        		if(!$data['bank_mobile'])throw new Exception('请输入银行预留手机号');
        		if(!$data['bank_user'])throw new Exception('请输入开户人');
        	}
        	$url_str = 'http://'.$_SERVER['HTTP_HOST'];
        	//判断图片中是否包含url连接有的话，去除连接
        	if(strpos($data['ali'],$url_str) !== false){
        		$data['ali'] = str_replace($url_str,"",$data['ali']);
        	}
        	if(strpos($data['wechat'],$url_str) !== false){
        		$data['wechat'] = str_replace($url_str,"",$data['wechat']);
        	}
        	
        	$data['update_time'] = time();
        	//修改信息
        	if($pay_info) {
        		UserPay::where(['user_id' => $this->user_id])->update($data);
        	} else {
        		//添加操作
        		$data['create_time'] = time();
        		$data['user_id'] = $this->user_id;
        		UserPay::create($data);
        	}
        	
        } catch (Exception $e) {
        	return jsonError($e->getMessage());
        }
        return jsonSuccess('提交成功');
    }
    /**
     * 上传收款码
     */
    public function payment()
    {
    	//获取客户端传递的临时文件
    	if(!empty($_FILES['file'])){
    		$file = $_FILES['file'];
    		$upload = new Base64();
    		try {
    			$res = $upload->uniAppUpload(Config::get('path.payment'), $file);
    			if(!$res)throw new Exception('图片上传失败');
    		} catch (Exception $e) {
    			return jsonError($e->getMessage());
    		}
    		return jsonSuccess($res);
    	}
    }
    /**
     * 修改登录密码
     */
    public function password(){
        try {
        	$data = $this->reqeust->post('');
        	//旧密码验证
        	$password = Users::where(['id'=>$this->user_id])->value('password');
        	if($password !== md5($data['old_password']))throw new Exception('旧密码输入错误');
        	$new_password=md5($data['new_password']);
        	Users::where(['id'=>$this->user_id])->update(['password'=>$new_password,'update_time'=>time()]);
        } catch (Exception $e) {
        	return jsonError($e->getMessage());
        }
        session('user_id',null);
        return jsonSuccess('修改成功');
    }
    /**
     * 修改交易密码
     */
    public function pay_pwd(){
        try {
        	$data = $this->reqeust->post('');
        	//旧密码验证
        	$password = Users::where(['id'=>$this->user_id])->value('pay_pwd');
        	if($password !== md5($data['old_password']))throw new Exception('旧密码输入错误');
        	$new_password=md5($data['new_password']);
        	Users::where(['id'=>$this->user_id])->update(['pay_pwd'=>$new_password,'update_time'=>time()]);
        } catch (Exception $e) {
        	return jsonError($e->getMessage());
        }
        return jsonSuccess('修改成功');
    }
    
    /**
     * 上传反馈图片
     */
    public function feedup()
    {
    	//获取客户端传递的临时文件
    	if(!empty($_FILES['file'])){
    		$file = $_FILES['file'];
    		$upload = new Base64();
    		try {
    			$res = $upload->uniAppUpload(Config::get('path.feed'), $file);
    			if(!$res)throw new Exception('图片上传失败');
    		} catch (Exception $e) {
    			return jsonError($e->getMessage());
    		}
    		return jsonSuccess($res);
    	}
    }
    /**
     * 用户反馈
     * @throws Exception
     * @return unknown
     */
    public function userFeeds()
    {
    	$info = $this->request->post('');
    	$content = $info['content'];
    	$img = $info['img'];
    	$data = [
    			'user_id' => $this->user_id,
    			'content' => $content,
    			'create_time' => time()
    	];
    	try {
    		if(!$img)$img = [];
    		$data['img'] = json_encode($img);
    		if(!$content)throw new Exception('反馈内容不能为空');
    		UserFeed::create($data);
    	} catch (Exception $e) {
    		return jsonError($e->getMessage());
    	}
    	return jsonSuccess('反馈成功');
    }
}